package com.groupbuying.storeinternal.mq;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.groupbuying.storeinternal.service.StoreOpexRuleRelService;
import com.groupbuying.storeinternal.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @anthor wenguoqiang
 * @date: 2019/06/10 8:57
 * @company 够样
 * @des
 **/
@Component
public class StoreInternalResultListener implements MessageListener {

    public static final Logger LOGGER = LoggerFactory.getLogger(StoreInternalResultListener.class);

    @Autowired
    private StoreOpexRuleRelService storeOpexRuleRelService;


    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {

        //Topic校验Tag校验
        if (!message.getTopic().equals(StoreInternalMQConfig.TOPIC) || StringUtils.isEmpty(message.getTag())) {
            LOGGER.error("Topic不符合订阅要求，Topic：" + message.getTopic() + "或者Tag为空，Tag：" + message.getTag());
            return Action.CommitMessage;
        }
        //获取消息重试次数
        int reconsume = message.getReconsumeTimes();
        if (reconsume == 3) {
            LOGGER.error("该消息重复消费三次，直接标识成功");
            return Action.CommitMessage;

        }
        //解析消息体
        //Map<String, Object> result = JSONUtils.jsonToMap(new String(message.getBody()));
        /**
         * 通过TAG来判断处理方式
         *
         */
        //LOGGER.info("获取消息：" + result.toString());
        //微信支付回执处理
        if (StoreInternalMQConfig.UPDATERULE.equals(message.getTag())) {
            storeOpexRuleRelService.batchPushPreOpexToMaster();
        }
        return  Action.CommitMessage;
    }
}
